from django.shortcuts import render, redirect
from django.views import View
from django import http
from django.contrib.auth import login, logout, authenticate
import re
from .models import UserProfile
from django_redis import get_redis_connection
from utils.response_code import RETCODE

from users.forms import RegisterForm


# 首页
class IndexView(View):
    def get(self, request):
        """
        # all_banners 获取轮播图表(Banner)所有轮播图数据
        # courses  获取课程信息表(Course)中的课程信息数据
        # banner_courses 获取课程信息表中(Course)的课程信息数据 和上面数据一样
        # course_orgs  获取课程学校表(CourseOrg)中的数据
        """
        return render(request, 'index.html')


# 注册
class RegisterView(View):
    def get(self, request):
        """
        响应注册页面
        :param request:
        :return:
        """
        # 1.接受,验证,响应,处理
        # 生成表单对象
        register_form = RegisterForm()
        return render(request, 'register.html', {'register_form': register_form})

    # 判断发送的表是否正确
    def post(self, request):
        """
        验证表单数据
        """
        # 1、获取前端传递的表单数据
        data = request.POST
        email = data.get('email')
        password = data.get('password')
        captcha = data.get('captcha')

        # 2、验证表单数据
        register_form = RegisterForm(data)
        res = register_form.is_valid()  # 验证成功返回True，验证失败返回False
        if res:
            # 验证成功，则执行相应业务逻辑操作，这里就直接返回验证成功后的字段数据
            # 3.处理
            # 3.1保存对象
            user = UserProfile.objects.create_user(username=email, email=email, password=password)

            # 3.2状态保持
            login(request, user)

            # 4.响应
            return render(request, 'index.html')
        else:
            # 验证失败，则在注册模板中通过register_form.errors获取错误
            return render(request, 'register.html', {'register_form': register_form})


# 登录
# class LoginView(View):
#     def get(self, request):
#         return render(request, 'login.html')
#
#     def post(self, request):
#         # 1.接收
#         email = request.POST.get('email')
#         password = request.POST.get('password')
#         next_url = request.POST.get('next', '/')
#
#         # 2.验证
#         # 2.1非空
#         if not all([email, password]):
#             return http.HttpResponseBadRequest('参数不完整')
#         # 2.2邮箱
#         if not re.match('^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
#             return http.JsonResponse({
#                 'code': RETCODE.PARAMERR,
#                 'errmsg': '邮箱格式有误'
#             })
#         # 2.4密码
#         if not re.match('^[0-9A-Za-z]{8,20}$', password):
#             return http.HttpResponseBadRequest('请输入8-20位的密码')
#
#         # 3.处理:查询　状态保持
#         user = authenticate(email=email, password=password)
#
#         if user is None:
#             return render(request, 'login.html', {
#                 'loginerror': '用户名或密码错误'
#             })
#         else:
#             # 状态保持
#             login(request, user)
#
#             # 输出cookie　用于前端提示
#             response = redirect(next_url)
#             response.set_cookie('username', user.username, max_age=60 * 60 * 24 * 14)
#
#             # 4.响应
#             return response
#
